package com.cdu.service.impl;

import com.cdu.commons.ServiceCode;
import com.cdu.commons.ServiceException;
import com.cdu.mapper.ProductMapper;
import com.cdu.pojo.dto.ProductQueryDTO;
import com.cdu.pojo.entity.User;
import com.cdu.pojo.vo.PageVO;
import com.cdu.pojo.vo.ProductVO;
import com.cdu.service.ProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: cdu-app
 * @Titile: ProductServiceImpl
 * @Author: Administrator
 * @Description: 商品业务实现类
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Override
    public PageVO<ProductVO> getProductListByPage(ProductQueryDTO productQueryDTO, User user) {
        //判断分类id是否存在
        if(productQueryDTO==null || productQueryDTO.getCategoryId()==null){
            throw new ServiceException("分类id不能为空", ServiceCode.REQUEST_ERROR);
        }
        //设置分页参数
        PageHelper.startPage(productQueryDTO.getPageNum(), productQueryDTO.getPageSize());
        //判断用户的编号
        Integer userId = user!=null ? user.getId() : null;
        Page<ProductVO> list = (Page<ProductVO>) productMapper.getProductListByCategoryId(productQueryDTO.getCategoryId(), userId);
        if(list == null || list.isEmpty()){
            throw new ServiceException("没有查询到数据",ServiceCode.DATA_NOT_FOUND);
        }
        //重新封装成PageVO
        PageVO<ProductVO> pageVO = new PageVO<>();
        pageVO.setPageNum(list.getPageNum());//页码值
        pageVO.setPageSize(list.getPageSize());//每页数据量
        pageVO.setTotal(list.getTotal());//总数据量
        pageVO.setPages(list.getPages());//总页码
        pageVO.setPageData(list.getResult());//具体数据
        return pageVO;
    }
}
